package com.bschlearn.group.service;

import org.jboss.logging.Logger;

import com.bschlearn.group.message.BaseReq;
import com.bschlearn.group.message.BaseRsp;
import com.bschlearn.group.model.User;

/**
 * 
 * @Title: service 
 * @Description: TODO(总项目基类服务方法) 
 * @param @param req
 * @param @return
 * @param @throws Exception  参数说明 
 * @return BaseRsp    返回类型 
 * @created 2019年10月31日 下午3:05:02 创建时间
 * @author dcc
 * @throws
 */
public abstract class BaseService<T extends BaseReq, V extends BaseRsp> {
	
	protected Logger logger = Logger.getLogger(getClass());
	
	private BaseRsp rsp;
	
	public User user ;

    public BaseRsp service(T req) throws Exception
    {
        logger.debug(req.getClass().getName() + ":" + req);

        user = (User) req.getRequest().getSession().getAttribute("user");
        
        try
        {
            logger.info(" into " + getClass().getName() + " ...");
            rsp = subService(req);

            logger.info("exit " + getClass().getName() + " ...");
            logger.debug("response:" + rsp);

            if (null != rsp)
            {
                return rsp;
            }

            return BaseRsp.ok(rsp.getData());
        }
        catch (Exception e)
        {
            logger.error("group exception ：" + e.getMessage(), e);
            return BaseRsp.errorException(e.getMessage());
        }
    }

    /**
     * 实现子类service各自的逻辑
     * @param req T 请求类
     * @return V 响应类的泛型
     * @throws MorliaException 异常
     */ 
    abstract protected V subService(T req) throws Exception;

}
